<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑  包裹动画过度 -->
      <XTXMianBaoxie :Path="`/FenLei/${quanbufenlei.id}`"  :Name="quanbufenlei.name"></XTXMianBaoxie>
      <!-- 轮播图 -->
      <XTXLunboBanner :lunbotu="lunbotu" style="height: 500px" />
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in quanbufenlei.children" :key="item.id">
            <a href="javascript:;">
              <img
                :src="item.picture"
              />
              <p>{{item.name}}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 不同分类商品 顶级类目-分类商品-布局-->
        <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in shangpin" :key="item.id">
        <div class="head">
          <h3>- {{item.name}} -</h3>
          <p class="tag">温暖柔软 品质之选</p>
          <XTXGenDuo />
        </div>
        <div class="body">
          <FenleiItem v-for="goods in item.goods" :key="goods.id" :goods="goods"  />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import FenleiItem from './components/fenlei-item.vue'
import { lunbotuqinqiu, zhanshiqinqiu } from '@/Api/index'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { computed, ref, watch } from 'vue'
export default {
  name: 'TopCategory',
  components: { FenleiItem },
  setup () {
    // 轮播图
    const lunbotu = ref([])
    lunbotuqinqiu().then((data) => {
      lunbotu.value = data.result
    })
    // 面包屑+所有分类
    const store = useStore()
    const route = useRoute()
    const quanbufenlei = computed(() => {
      let fenleishuoju = {}
      // 还回购物车模块数据
      const item = store.state.gouwuche.gouwuche.find(item => {
        return item.id === route.params.id
      })
      // 先判断是否有数据 有就加
      if (item) fenleishuoju = item
      return fenleishuoju
    })
    // 推荐商品
    const shangpin = ref([])
    // 数据请求 封装函数里面
    const hanshuqinqiu = () => {
      zhanshiqinqiu(route.params.id).then(data => {
        shangpin.value = data.result.children
      })
    }
    // 监听 路由id 有变动，就重新加载数据请求
    watch(() => route.params.id, (newVal) => {
      // newVal && hanshuqinqiu()
      if (newVal && `/FenLei/${newVal}` === route.path) hanshuqinqiu()
    }, { immediate: true })
    return { lunbotu, quanbufenlei, shangpin }
  }
}
</script>
<style scoped lang="less">
.top-category {
  width: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  .container {
    width: 1240px;
      .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }
    h3 {
      font-size: 28px;
      color: #666;
      font-weight: normal;
      text-align: center;
      line-height: 100px;
    }
    .sub-list {
      margin-top: 20px;
      background-color: #fff;
      ul {
        display: flex;
        padding: 0 32px;
        flex-wrap: wrap;
        li {
          width: 168px;
          height: 160px;
          a {
            text-align: center;
            display: block;
            font-size: 16px;
            img {
              width: 100px;
              height: 100px;
            }
            p {
              line-height: 40px;
            }
            &:hover {
              color: @xtxColor;
            }
          }
        }
      }
    }
  }
}
</style>
